package ystp.support;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class CreateWord {
	
	
	public static void createWord(String userfilepath,String filepath,String filename,Map<String,Object> dataMap,
			HttpServletRequest request,HttpServletResponse response,String ppFlag){
		DocUtil doc = new DocUtil();
		Writer out = null;
		try {
			File dir = new File(filepath);
			if(!dir.exists()){
				dir.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(filepath + filename);
			out = new OutputStreamWriter(fos,"utf-8");
			String mmMuBanMingCheng="";
			String templatepath = "";
			
			if ("zjcq".equals(ppFlag)) {
				mmMuBanMingCheng="zjcqjl.xml";
				templatepath = userfilepath + "/muban/zjcq/";
			}else if ("xiangjiang".equals(ppFlag)) {
				mmMuBanMingCheng="HongKong_Sheet.xml";
				templatepath = userfilepath + "/muban/xiangjiang/";
			}
			else if ("xueshujiaoliu".equals(ppFlag)) {
				mmMuBanMingCheng="xueshujiaoliu.xml";
				templatepath = userfilepath + "/muban/xueshujiaoliu/";
			}
			else if ("xueshujiaoliuhuizongbiao".equals(ppFlag)) {
				mmMuBanMingCheng="xueshujiaoliuhuizongbiao.xml";
				templatepath = userfilepath + "/muban/xueshujiaoliu/";
			}
			else if ("aomenxuezhe".equals(ppFlag)) {
				mmMuBanMingCheng="aomenxuezheshenqingbiao.xml";
				templatepath = userfilepath + "/muban/aomenxuezhe/";
			}
			else if ("paichu".equals(ppFlag)) {
				mmMuBanMingCheng="GJJL.xml";
				templatepath = userfilepath + "/muban/guojijiaoliu/";
			}
			else if ("zhongde".equals(ppFlag)) {
				mmMuBanMingCheng="Zhongde.xml";
				templatepath = userfilepath + "/muban/zhongde/";
			}
			doc.write(templatepath, mmMuBanMingCheng, dataMap, out);
			reutrnfile(filepath,filename,request,response);
			//return filepath + filename;
		} catch (Exception e) {
			throw new RuntimeException("createWord"+e.getMessage());
		}
	}
	
	
	public static void reutrnfile(String filepath,String filename,
			HttpServletRequest request,HttpServletResponse response) throws IOException {
		ServletOutputStream out = null;
		FileInputStream ips = null;
		try {
			
			// 获取文件存放的路径
			File file = new File(filepath + filename);
			String newFileName = filename;
			if (!file.exists()) {
				// 如果文件不存在就跳出
				return;
			}
			ips = new FileInputStream(file);
			response.setContentType("multipart/form-data");
			// 为文件重新设置名字，采用数据库内存储的文件名称
			response.addHeader("Content-Disposition", "attachment; filename=\""
					+ newFileName
					+ "\"");
			out = response.getOutputStream();
			// 读取文件流
			int len = 0;
			byte[] buffer = new byte[1024 * 10];
			while ((len = ips.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
			out.flush();
			ips.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	

}
